<template>
  <div class="web-container search" v-loading="loading">
    <span>共{{total}}条 <span class="search-font">{{$route.query.searchValue}}</span> 相关的搜索结果</span>
    <div class="search-area">
      <div class="block" v-if="isShow">
        <book :list="list" v-if="$route.query.searchType == 1"></book>
        <periodical :list="list" v-else-if="$route.query.searchType == 5"></periodical>
        <newspaper :list="list" v-else-if="$route.query.searchType == 4"></newspaper>
        <videomusic :list="list" v-else-if="$route.query.searchType == 7"></videomusic>
        <read :list="list" v-else-if="$route.query.searchType==2"></read>
         <div class="pagination">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="10" layout="total, prev, pager, next, jumper" :total="total">
                </el-pagination>
            </div>
      </div>
      <div v-else class="search-null">
        <img src="/static/images/base/search-null.png" alt="">
        <span>很抱歉没有找到与&nbsp;"{{$route.query.searchValue}}"&nbsp;相关的资源</span>
      </div>
    </div>
  </div>
</template>

<script>
import book from "@/view/search/book";
import periodical from "@/view/search/periodical";
import newspaper from "@/view/search/newspaper";
import read from "@/view/search/read";
import videomusic from "@/view/search/video_music";
import { search } from "@/api/index";
export default {
  data() {
    return {
      list: [],
      currentPage: Number(this.$route.query.currentPage)||1,
      search_form: {},
      total:0,
      loading:false
    };
  },
  components: {
    book,
    periodical,
    newspaper,
    read,
    videomusic
  },
  created() {
    this.init(this.$route.query.searchValue,this.$route.query.searchType,this.$route.query.currentPage);
  },
  watch: {
   '$route'(){
      this.init(this.$route.query.searchValue,this.$route.query.searchType,this.$route.query.currentPage);
      console.log(123)
    }
  },
  methods: {
    init(searchValue,searchType,currentPage,pageSize) {
      this.loading = true
      search(searchValue,searchType,currentPage,pageSize)
        .then(res => {
          this.total = res.data.total;
          this.list = res.data.data
          this.loading = false
        })
        .catch(err => {});
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.$router.push(this.$route.path+'?currentPage='+this.currentPage+'&searchValue='+this.$route.query.searchValue+'&searchType='+this.$route.query.searchType)
      console.log(`当前页: ${val}`);
    }
  },
  computed: {
    isShow() {
      if (this.total == 0) {
        return false;
      } else {
        return true;
      }
    }
  }
};
</script>
<style lang="less" scoped>
.search {
  margin-top: 20px;
  .pagination{
    padding-bottom: 20px;
  }
  .search-font {
    color: #15c798;
    font-weight: bold;
  }
  .search-area {
    margin-top: 20px;
    // padding: 0px 20px 20px 20px;
    background: #fff;
    border-radius: 10px;
    min-height: 300px;
  }
  .search-null {
    min-height: 400px;
    text-align: center;
    img {
      margin-top: 50px;
    }
  }
}
</style>


